#pragma once

#include "resource.h"       // main symbols
#include <atlhost.h>
#include "XMLParser/EBomTreeNode.h"

// CAssemblyTreeDlg
class CV5RdWorks;

class CAssemblyTreeDlg : 
	public CAxDialogImpl<CAssemblyTreeDlg>
{
public:
	CAssemblyTreeDlg(EBomTreeNode* pRootNode, CV5RdWorks* pReader):
	  m_ctlSysTreeView32(_T("SysTreeView32"), this, 1)
	{
		m_treeRootNode = pRootNode;
		m_pV5Reader = pReader;
	}

	~CAssemblyTreeDlg()
	{ 
		if(m_hRightMenu != NULL)
			DestroyMenu(m_hRightMenu);

		if(m_hImageList != NULL)
			ImageList_Destroy(m_hImageList);
	}

	enum { IDD = IDD_ASMTREE_DLG };

BEGIN_MSG_MAP(CAssemblyTreeDlg)
	MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
	COMMAND_HANDLER(IDOK, BN_CLICKED, OnClickedOK)
	COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnClickedCancel)
	CHAIN_MSG_MAP(CAxDialogImpl<CAssemblyTreeDlg>)
	MESSAGE_HANDLER(WM_SIZE, OnSize)
ALT_MSG_MAP(1)
	// Replace this with message map entries for superclassed SysTreeView32
	//MESSAGE_HANDLER(WM_LBUTTONDBLCLK, OnLButtonDblClk)
	//MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
	//MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
	COMMAND_ID_HANDLER(ID_OPEN_ASMPART, OnOpen)
	MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRButtonDown)
END_MSG_MAP()

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

	LRESULT OnRButtonDown(UINT uMsg, WPARAM wParam,
		LPARAM lParam, BOOL& bHandled);

	LRESULT OnOpen(WORD wNotifyCode, WORD wID, HWND hWndCtlr, BOOL& bHandled);

	LRESULT OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

	LRESULT OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		EndDialog(wID);
		return 0;
	}

	HTREEITEM InsertItem(HWND hWnd, LPCTSTR lpszItem, int iImage, int iSelectImage, LPARAM lParam,
		HTREEITEM hParent = TVI_ROOT, HTREEITEM hAfter = TVI_LAST)
	{
		_ASSERTE(::IsWindow(hWnd));
		TV_INSERTSTRUCT tvis;
		::ZeroMemory(&tvis, sizeof(TV_INSERTSTRUCT));

		tvis.hParent = hParent;
		tvis.hInsertAfter = hAfter;
		tvis.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
		tvis.item.cchTextMax = 80;
		tvis.item.pszText = (LPTSTR)lpszItem;
		tvis.item.iImage = 0;
		tvis.item.iSelectedImage = 1;
		tvis.item.state = 0;
		tvis.item.stateMask = 0;
		tvis.item.lParam = lParam;
		return (HTREEITEM)::SendMessage(hWnd, TVM_INSERTITEM, 0, (LPARAM)&tvis);
	}

	void SetLParam(HTREEITEM hItem, LPARAM lParam)
	{
		TV_ITEM tvi;

		tvi.mask = TVIF_PARAM;
		tvi.hItem = hItem;
		tvi.lParam = lParam;
		TreeView_SetItem(m_ctlSysTreeView32.m_hWnd, &tvi);
	}

	// Retrieve the number which the function above use to set.
	void GetLParam(HTREEITEM hItem, LPARAM& lParam)
	{
		TV_ITEM tvi;

		tvi.mask = TVIF_PARAM;
		tvi.hItem = hItem;
		TreeView_GetItem(m_ctlSysTreeView32.m_hWnd, &tvi);
		lParam = tvi.lParam;
	}

	void CreateTreeView();

	void DrawAssemblyTree();

	void AddChildNode(EBomTreeNode** ppChildNodes,
		int iChildNum, HTREEITEM& rootItem);

	void CreatePopup();

	LRESULT OnDraw(ATL_DRAWINFO& di)
	{
		RECT& rc = *(RECT*)di.prcBounds;
		DrawEdge(di.hdcDraw, &rc, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
		return S_OK;
	}

private:
	CV5RdWorks*				m_pV5Reader;
	BOOL					m_bAssembly;
	TCHAR*					m_strOldLogPathName;

	CContainedWindow		m_ctlSysTreeView32;
	HIMAGELIST				m_hImageList;
	TV_INSERTSTRUCT			TreeCtrlItem;

	OLE_COLOR				m_oleFourColor;
	COLORREF				m_clrFourColor;

	HTREEITEM				m_hAtRoot;
	HTREEITEM				m_hti;
	HTREEITEM				m_hChildItem;
	HTREEITEM				m_hTreeItemDrop;
	HTREEITEM				m_hTreeItemOldDrop;
	HTREEITEM				m_hTreeItemDrag;
	HTREEITEM				m_htiKillFocus;
	EBomTreeNode*			m_treeRootNode;

	HMENU					m_hRightMenu;
	HMENU					m_hLoadMenu;
public:
	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
};


